import sys
import pygame

class ASTree:
    def __init__(self, width, height):
        nw = Point(-5,-5)
        ne = Point(width+5, -5)
        se = Point(width+5, height+5)
        sw = Point(-5, width+5)
        nw.neigh['e'] = ne
        nw.neigh['s'] = sw
        ne.neigh['w'] = nw
        ne.neigh['s'] = se
        se.neigh['n'] = ne
        se.neigh['w'] = sw
        sw.neigh['e'] = se
        sw.neigh['n'] = nw
        self.root = Area(nw, ne, se, sw)

    def addSplit(self, x, y, t=False):
        return self.root.addSplit(x, y, t)

    def getLeafs(self):
        return self.root.getSubAreas()

    def addLineSplit(self,s,o,t=False):
        self.root.addLineSplit(s,o,t)

    def cleanMap(self):
        self.root.cleanTemps()

class Area:
    def __init__(self,nw,ne,se,sw,parent=None):
        self.points = {'nw':nw, 'ne':ne, 'se':se, 'sw':sw}
        self.parent = parent
        self.split = None
        self.subArea = {'nw':None, 'ne':None, 'se':None, 'sw':None}
        self.check = [False,False]

    def getArea(self,point):
        if self.split is None:
            return [self]
        else:
            if point.x == self.split.x:
                if point.y == self.split.y:
                    t = []
                    if self.subArea['nw'] != None:
                        t += self.subArea['nw'].getArea(point)
                    if self.subArea['ne'] != None:
                        t += self.subArea['ne'].getArea(point)
                    if self.subArea['se'] != None:
                        t += self.subArea['se'].getArea(point)
                    if self.subArea['sw'] != None:
                        t += self.subArea['sw'].getArea(point)
                    return t
                elif point.y > self.split.y:
                    t = []
                    if self.subArea['se'] != None:
                        t += self.subArea['se'].getArea(point)
                    if self.subArea['sw'] != None:
                        t += self.subArea['sw'].getArea(point)
                    return t
                else:
                    t = []
                    if self.subArea['ne'] != None:
                        t += self.subArea['ne'].getArea(point)
                    if self.subArea['nw'] != None:
                        t += self.subArea['nw'].getArea(point)
                    return t
            elif point.x > self.split.x:
                if point.y == self.split.y:
                    t = []
                    if self.subArea['se'] != None:
                        t += self.subArea['se'].getArea(point)
                    if self.subArea['ne'] != None:
                        t += self.subArea['ne'].getArea(point)
                    return t
                elif point.y > self.split.y:
                    t = []
                    if self.subArea['se'] != None:
                        t += self.subArea['se'].getArea(point)
                    return t
                else:
                    t = []
                    if self.subArea['ne'] != None:
                        t += self.subArea['ne'].getArea(point)
                    return t
            elif point.x < self.split.x:
                if point.y == self.split.y:
                    t = []
                    if self.subArea['sw'] != None:
                        t += self.subArea['sw'].getArea(point)
                    if self.subArea['nw'] != None:
                        t += self.subArea['nw'].getArea(point)
                    return t
                elif point.y > self.split.y:
                    t = []
                    if self.subArea['sw'] != None:
                        t += self.subArea['sw'].getArea(point)
                    return t
                else:
                    t = []
                    if self.subArea['nw'] != None:
                        t += self.subArea['nw'].getArea(point)
                    return t

    def addSplit(self,x,y,temp):
        point = Point(x,y)
        areas = self.getArea(point)

        if len(areas) == 0:
            pass
        elif len(areas) == 1:
            area = areas[0]
            area.split = point
            #Create points
            n = Point(point.x, area.points['nw'].y)
            w = Point(area.points['nw'].x, point.y)
            s = Point(point.x, area.points['se'].y)
            e = Point(area.points['se'].x, point.y)
            if temp:
                area.check[1] = True
                a1 = area.parent
                while a1 != None:
                    a1.check[0] = True
                    a1 = a1.parent
                point.temp = True
                n.temp = True
                n.recon = ['w','e']
                w.temp = True
                w.recon = ['n','s']
                s.temp = True
                s.recon = ['w','e']
                e.temp = True
                e.recon = ['n','s']
            #Create Areas (nw,ne,se,sw,parent)
            area.subArea['nw'] = Area(area.points['nw'],n,point,w,area)
            area.subArea['ne'] = Area(n,area.points['ne'],e,point,area)
            area.subArea['se'] = Area(point,e,area.points['se'],s,area)
            area.subArea['sw'] = Area(w,point,s,area.points['sw'],area)
            #Fix connections
            point.neigh['n'] = n
            point.neigh['w'] = w
            point.neigh['s'] = s
            point.neigh['e'] = e
            n.neigh['s'] = point
            w.neigh['e'] = point
            s.neigh['n'] = point
            e.neigh['w'] = point
            (p1,p2) = area.points['nw'].getInterval('e',point)
            p1.neigh['e'] = n
            p2.neigh['w'] = n
            n.neigh['w'] = p1
            n.neigh['e'] = p2
            (p1,p2) = area.points['ne'].getInterval('s',point)
            p1.neigh['s'] = e
            p2.neigh['n'] = e
            e.neigh['n'] = p1
            e.neigh['s'] = p2
            (p1,p2) = area.points['se'].getInterval('w',point)
            p1.neigh['w'] = s
            p2.neigh['e'] = s
            s.neigh['e'] = p1
            s.neigh['w'] = p2
            (p1,p2) = area.points['sw'].getInterval('n',point)
            p1.neigh['n'] = w
            p2.neigh['s'] = w
            w.neigh['s'] = p1
            w.neigh['n'] = p2
            return point
        elif len(areas) == 2:
            areas[0].split = point
            areas[1].split = point
            if areas[0].points['nw'].x == point.x:
                n = areas[0].points['nw']
                w = Point(areas[1].points['nw'].x, point.y)
                s = areas[1].points['se']
                e = Point(areas[0].points['se'].x, point.y)
                if temp:
                    for i in [0,1]:
                        areas[i].check[1] = True
                        a1 = areas[i].parent
                        while a1 != None:
                            a1.check[0] = True
                            a1 = a1.parent
                    point.temp = True
                    point.recon = ['n','s']
                    w.temp = True
                    w.recon = ['n','s']
                    e.temp = True
                    e.recon = ['n','s']
                #Create Areas (nw,ne,se,sw,parent)
                areas[0].subArea['ne'] = Area(n,areas[0].points['ne'],e,point,areas[0])
                areas[0].subArea['se'] = Area(point,e,areas[0].points['se'],areas[0].points['sw'],areas[0])
                areas[1].subArea['nw'] = Area(areas[1].points['nw'],areas[1].points['ne'],point,w,areas[1])
                areas[1].subArea['sw'] = Area(w,point,s,areas[1].points['sw'],areas[1])
                #Fix connections
                point.neigh['w'] = w
                point.neigh['e'] = e
                w.neigh['e'] = point
                e.neigh['w'] = point
                (p1,p2) = areas[0].points['nw'].getInterval('s',point)
                p1.neigh['s'] = point
                p2.neigh['n'] = point
                point.neigh['n'] = p1
                point.neigh['s'] = p2
                (p1,p2) = areas[0].points['se'].getInterval('n',point)
                p1.neigh['n'] = e
                p2.neigh['s'] = e
                e.neigh['s'] = p1
                e.neigh['n'] = p2
                (p1,p2) = areas[1].points['nw'].getInterval('s',point)
                p1.neigh['s'] = w
                p2.neigh['n'] = w
                w.neigh['n'] = p1
                w.neigh['s'] = p2
                return point
            elif areas[0].points['nw'].y == point.y:
                n = Point(point.x, areas[1].points['nw'].y)
                w = areas[0].points['nw']
                s = Point(point.x, areas[0].points['se'].y)
                e = areas[1].points['se']
                if temp:
                    for i in [0,1]:
                        areas[i].check[1] = True
                        a1 = areas[i].parent
                        while a1 != None:
                            a1.check[0] = True
                            a1 = a1.parent
                    point.temp = True
                    point.recon = ['w','e']
                    n.temp = True
                    n.recon = ['w','e']
                    s.temp = True
                    s.recon = ['w','e']
                #Create Areas (nw,ne,se,sw,parent)
                areas[0].subArea['sw'] = Area(w,point,s,areas[0].points['sw'],areas[0])
                areas[0].subArea['se'] = Area(point,areas[0].points['ne'],areas[0].points['se'],s,areas[0])
                areas[1].subArea['nw'] = Area(areas[1].points['nw'],n,point,areas[1].points['sw'],areas[1])
                areas[1].subArea['ne'] = Area(n,areas[1].points['ne'],e,point,areas[1])
                #Fix connections
                point.neigh['n'] = n
                point.neigh['s'] = s
                n.neigh['s'] = point
                s.neigh['n'] = point
                (p1,p2) = areas[0].points['nw'].getInterval('e',point)
                p1.neigh['e'] = point
                p2.neigh['w'] = point
                point.neigh['w'] = p1
                point.neigh['e'] = p2
                (p1,p2) = areas[0].points['se'].getInterval('w',point)
                p1.neigh['w'] = s
                p2.neigh['e'] = s
                s.neigh['e'] = p1
                s.neigh['w'] = p2
                (p1,p2) = areas[1].points['nw'].getInterval('e',point)
                p1.neigh['e'] = n
                p2.neigh['w'] = n
                n.neigh['w'] = p1
                n.neigh['e'] = p2
                return point
            elif areas[0].points['se'].x == point.x:
                n = areas[1].points['nw']
                w = Point(areas[0].points['nw'].x, point.y)
                s = areas[0].points['se']
                e = Point(areas[1].points['se'].x, point.y)
                if temp:
                    for i in [0,1]:
                        areas[i].check[1] = True
                        a1 = areas[i].parent
                        while a1 != None:
                            a1.check[0] = True
                            a1 = a1.parent
                    point.temp = True
                    point.recon = ['n','s']
                    w.temp = True
                    w.recon = ['n','s']
                    e.temp = True
                    e.recon = ['n','s']
                #Create Areas (nw,ne,se,sw,parent)
                areas[1].subArea['ne'] = Area(n,areas[1].points['ne'],e,point,areas[1])
                areas[1].subArea['se'] = Area(point,e,areas[1].points['se'],areas[1].points['sw'],areas[1])
                areas[0].subArea['nw'] = Area(areas[0].points['nw'],areas[0].points['ne'],point,w,areas[0])
                areas[0].subArea['sw'] = Area(w,point,s,areas[0].points['sw'],areas[0])
                #Fix connections
                point.neigh['w'] = w
                point.neigh['e'] = e
                w.neigh['e'] = point
                e.neigh['w'] = point
                (p1,p2) = areas[1].points['nw'].getInterval('s',point)
                p1.neigh['s'] = point
                p2.neigh['n'] = point
                point.neigh['n'] = p1
                point.neigh['s'] = p2
                (p1,p2) = areas[1].points['se'].getInterval('n',point)
                p1.neigh['n'] = e
                p2.neigh['s'] = e
                e.neigh['s'] = p1
                e.neigh['n'] = p2
                (p1,p2) = areas[0].points['nw'].getInterval('s',point)
                p1.neigh['s'] = w
                p2.neigh['n'] = w
                w.neigh['n'] = p1
                w.neigh['s'] = p2
                return point
            elif areas[0].points['se'].y == point.y:
                n = Point(point.x, areas[0].points['nw'].y)
                w = areas[1].points['nw']
                s = Point(point.x, areas[1].points['se'].y)
                e = areas[0].points['se']
                if temp:
                    for i in [0,1]:
                        areas[i].check[1] = True
                        a1 = areas[i].parent
                        while a1 != None:
                            a1.check[0] = True
                            a1 = a1.parent
                    point.temp = True
                    point.recon = ['w','e']
                    n.temp = True
                    n.recon = ['w','e']
                    s.temp = True
                    s.recon = ['w','e']
                #Create Areas (nw,ne,se,sw,parent)
                areas[1].subArea['sw'] = Area(w,point,s,areas[1].points['sw'],areas[1])
                areas[1].subArea['se'] = Area(point,areas[1].points['ne'],areas[1].points['se'],s,areas[1])
                areas[0].subArea['nw'] = Area(areas[0].points['nw'],n,point,areas[0].points['sw'],areas[0])
                areas[0].subArea['ne'] = Area(n,areas[0].points['ne'],e,point,areas[0])
                #Fix connections
                point.neigh['n'] = n
                point.neigh['s'] = s
                n.neigh['s'] = point
                s.neigh['n'] = point
                (p1,p2) = areas[1].points['nw'].getInterval('e',point)
                p1.neigh['e'] = point
                p2.neigh['w'] = point
                point.neigh['w'] = p1
                point.neigh['e'] = p2
                (p1,p2) = areas[1].points['se'].getInterval('w',point)
                p1.neigh['w'] = s
                p2.neigh['e'] = s
                s.neigh['e'] = p1
                s.neigh['w'] = p2
                (p1,p2) = areas[0].points['nw'].getInterval('e',point)
                p1.neigh['e'] = n
                p2.neigh['w'] = n
                n.neigh['w'] = p1
                n.neigh['e'] = p2
                return point
        elif len(areas) == 3:
            corners = ['nw', 'ne', 'sw', 'se']
            t = []
            for a in areas:
                for c in corners:
                    if a.points[c].x == point.x and a.points[c].y == point.y:
                        t.append((a,c))
            if len(t) != 2:
                print "Error"
            else:
                areas.remove(t[0][0])
                areas.remove(t[1][0])
                areas[0].split = t[0][0].points[t[0][1]]
                if t[0][1][0] == t[1][1][0]:
                    if t[0][1][0] == 'n':
                        n = Point(point.x,areas[0].points['nw'].y)
                        if temp:
                            areas[0].check[1] = True
                            a1 = areas[0].parent
                            while a1 != None:
                                a1.check[0] = True
                                a1 = a1.parent
                            n.temp = True
                            n.recon = ['w','e']
                        #Create Areas (nw,ne,se,sw,parent)
                        areas[0].subArea['nw'] = Area(areas[0].points['nw'], n, t[0][0].points[t[0][1]], areas[0].points['sw'], areas[0])
                        areas[0].subArea['ne'] = Area(n, areas[0].points['ne'], areas[0].points['se'], t[0][0].points[t[0][1]], areas[0])
                        #Fix connections
                        t[0][0].points[t[0][1]].neigh['n'] = n
                        n.neigh['s'] = t[0][0].points[t[0][1]]

                        (p1,p2) = areas[0].points['nw'].getInterval('e', n)
                        p1.neigh['e'] = n
                        p2.neigh['w'] = n
                        n.neigh['w'] = p1
                        n.neigh['e'] = p2
                        return t[0][0].points[t[0][1]]
                    elif t[0][1][0] == 's':
                        s = Point(point.x,areas[0].points['sw'].y)
                        if temp:
                            areas[0].check[1] = True
                            a1 = areas[0].parent
                            while a1 != None:
                                a1.check[0] = True
                                a1 = a1.parent
                            s.temp = True
                            s.recon = ['w','e']
                        #Create Areas (nw,ne,se,sw,parent)
                        areas[0].subArea['sw'] = Area(areas[0].points['nw'], t[0][0].points[t[0][1]], s, areas[0].points['sw'], areas[0])
                        areas[0].subArea['se'] = Area(t[0][0].points[t[0][1]], areas[0].points['ne'], areas[0].points['se'], s, areas[0])

                        #Fix connections
                        t[0][0].points[t[0][1]].neigh['s'] = s
                        s.neigh['n'] = t[0][0].points[t[0][1]]

                        (p1,p2) = areas[0].points['sw'].getInterval('e', s)
                        p1.neigh['e'] = s
                        p2.neigh['w'] = s
                        s.neigh['w'] = p1
                        s.neigh['e'] = p2
                        return t[0][0].points[t[0][1]]
                    else:
                        print "Error"
                elif t[0][1][1] == t[1][1][1]:
                    if t[0][1][1] == 'w':
                        w = Point(areas[0].points['nw'].x, point.y)
                        if temp:
                            areas[0].check[1] = True
                            a1 = areas[0].parent
                            while a1 != None:
                                a1.check[0] = True
                                a1 = a1.parent
                            w.temp = True
                            w.recon = ['n','s']
                        #Create Areas (nw,ne,se,sw,parent)
                        areas[0].subArea['nw'] = Area(areas[0].points['nw'], areas[0].points['ne'], t[0][0].points[t[0][1]], w, areas[0])
                        areas[0].subArea['sw'] = Area(w, t[0][0].points[t[0][1]], areas[0].points['se'], areas[0].points['sw'], areas[0])
                        #Fix connections
                        t[0][0].points[t[0][1]].neigh['w'] = w
                        w.neigh['e'] = t[0][0].points[t[0][1]]

                        (p1,p2) = areas[0].points['nw'].getInterval('s', w)
                        p1.neigh['s'] = w
                        p2.neigh['n'] = w
                        w.neigh['n'] = p1
                        w.neigh['s'] = p2
                        return t[0][0].points[t[0][1]]
                    elif t[0][1][1] == 'e':
                        e = Point(areas[0].points['ne'].x, point.y)
                        if temp:
                            areas[0].check[1] = True
                            a1 = areas[0].parent
                            while a1 != None:
                                a1.check[0] = True
                                a1 = a1.parent
                            e.temp = True
                            e.recon = ['n','s']
                        #Create Areas (nw,ne,se,sw,parent)
                        areas[0].subArea['ne'] = Area(areas[0].points['nw'], areas[0].points['ne'], e, t[0][0].points[t[0][1]], areas[0])
                        areas[0].subArea['se'] = Area(t[0][0].points[t[0][1]], e, areas[0].points['se'], areas[0].points['sw'], areas[0])

                        #Fix connections
                        t[0][0].points[t[0][1]].neigh['e'] = e
                        e.neigh['w'] = t[0][0].points[t[0][1]]

                        (p1,p2) = areas[0].points['se'].getInterval('n', e)
                        p1.neigh['n'] = e
                        p2.neigh['s'] = e
                        e.neigh['s'] = p1
                        e.neigh['n'] = p2
                        return t[0][0].points[t[0][1]]
                    else:
                        print "Error"
                else:
                    print "Error"
        elif len(areas) == 4:
            for k,v in areas[0].points.iteritems():
                c = 0
                for a in areas[1:]:
                    for k2,v2 in a.points.iteritems():
                        if v2.x == v.x and v2.y == v.y:
                            c += 1
                            break
                if c == 3:
                    return v
            print '\n\n\n\nFAIL\n\n\n\n'
            if temp:
                point.temp = True
            return point

    def getSubAreas(self):
        if self.split is None:
            return [self]
        t = []
        for key in self.subArea:
            if  not (self.subArea[key] is None):
                t = t + self.subArea[key].getSubAreas()
        return t

    def addLineSplit(self, start, offset,temp):
        if  offset[0] > 0 and offset[1] == 0:
            p = self.addSplit(start[0], start[1],temp)
            while (p.neigh['e'].x < start[0]+offset[0]):
                p = self.addSplit(p.neigh['e'].x, p.neigh['e'].y,temp)
            self.addSplit(start[0]+offset[0], start[1],temp)
        elif  offset[0] == 0 and offset[1] > 0:
            p = self.addSplit(start[0], start[1],temp)
            while (p.neigh['s'].y < start[1]+offset[1]):
                p = self.addSplit(p.neigh['s'].x, p.neigh['s'].y,temp)
            self.addSplit(start[0], start[1]+offset[1],temp)
        else:
            print 'Invalid line definition'

    def cleanTemps(self):
        if self.check[0]:
            for k in self.subArea:
                if self.subArea[k] != None:
                    self.subArea[k].cleanTemps()
            self.check[0] = False
        if self.check[1]:
            for k in self.split.neigh:
                if self.split.neigh[k] != None:
                    self.split.neigh[k].reconnect()
            self.split.reconnect()
            self.split = None
            for k in self.subArea:
                if self.subArea[k] != None:
                    for k2 in self.subArea[k].points:
                        self.subArea[k].points[k2] = None
                    self.subArea[k] = None
            self.check[1] = False

class Point:
    def __init__(self, x=None, y=None):
        """AStree data"""
        self.x = x
        self.y = y
        self.neigh = {}
        self.temp = False
        self.recon = []

        """Theta * data"""
        self.x = x
        self.y = y
        self.val = 0
        self.g = 0
        self.parent = None

    def __eq__(self, other):
        return (self.x == other.x and self.y == other.y)

    def __lt__(self, other):
        return self.val < other.val

    def getInterval(self,dir,point):
        if dir == 'n':
            if (point.y >= self.neigh['n'].y):
                return (self,self.neigh['n'])
            else:
                return self.neigh['n'].getInterval(dir, point)
        elif dir == 'w':
            if (point.x >= self.neigh['w'].x):
                return (self,self.neigh['w'])
            else:
                return self.neigh['w'].getInterval(dir, point)
        elif dir == 's':
            if (point.y <= self.neigh['s'].y):
                return (self,self.neigh['s'])
            else:
                return self.neigh['s'].getInterval(dir, point)
        elif dir == 'e':
            if (point.x <= self.neigh['e'].x):
                return (self,self.neigh['e'])
            else:
                return self.neigh['e'].getInterval(dir, point)

    def loc(self):
        return (self.x, self.y)

    def reconnect(self):
        if self.temp:
            op = {'n':'s','w':'e','s':'n','e':'w'}
            for k in op:
                if k in self.neigh and not (self.neigh[k] is None):
                    if k in self.recon:
                        self.neigh[k].neigh[op[k]] = self.neigh[op[k]]
                    else:
                        self.neigh[k].neigh[op[k]] = None
            for k in op:
                if k in self.neigh:
                    self.neigh[k] = None

class ReadLevel:

    def __init__(self,levelLoc):
        f = open(levelLoc, 'r')
        ls = f.readlines()
        f.close()
        t = []
        f = False
        self.controls = []
        for l in ls:
            s = l.split()

            if len(s) != 0 and s[0] == 'WALLS':
                t.extend(s[2:])
                f = True
            elif f and s[0][0] == '#':
                pass
            elif f and "]" in s:
                t.extend(s)
                f = False
            elif f:
                t.extend(s)
            elif len(s) != 0 and s[0] == 'CONTROL_POINTS':
                self.controls = eval("".join(s[2:]))
            else:
                pass

        self.wallList = eval("".join(t))

    def createTree(self):
        controlsCopy = self.controls[:]
        self.walls = []
        self.tree = ASTree(800, 800)
        for w in self.wallList:
           # w = self.wallList.pop()
            #Fix offsets
            a = (w[0][0]-6, w[0][1]-6)
            a1 = (w[0][0]-5, w[0][1]-5)
            o = (w[1][0]+12, w[1][1]+12)
            o1 = (w[1][0]+10, w[1][1]+10)

            b = (a[0]+o[0], a[1]+o[1])
            b1 = (a1[0]+o1[0], a1[1]+o1[1])
            c = (a[0]+o[0], a[1])
            c1 = (a1[0]+o1[0], a1[1])
            d = (a[0], a[1]+o[1])
            d1 = (a1[0], a1[1]+o1[1])

            self.walls.append((a1,b1))
            self.walls.append((c1,d1))
            #corners.extend([a,b,c,d])

            self.tree.addLineSplit(a,(o[0],0))
            self.tree.addLineSplit(a,(0,o[1]))
            self.tree.addLineSplit(d,(o[0],0))
            self.tree.addLineSplit(c,(0,o[1]))
        for c in controlsCopy:
            #c = controlsCopy.pop()
            self.tree.addSplit(c[0],c[1])

def test():
    splits = []
    pygame.init()
    window = pygame.display.set_mode((500, 500))

    tree = ASTree(500, 500)

    leafs = tree.getLeafs()
    for leaf in leafs:
        pygame.draw.line(window, (255, 255, 255), leaf.points['nw'].loc(), leaf.points['ne'].loc())
        pygame.draw.line(window, (255, 255, 255), leaf.points['ne'].loc(), leaf.points['se'].loc())
        pygame.draw.line(window, (255, 255, 255), leaf.points['se'].loc(), leaf.points['sw'].loc())
        pygame.draw.line(window, (255, 255, 255), leaf.points['sw'].loc(), leaf.points['nw'].loc())


    pygame.display.update()
    t = False
    while True:
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)
            else:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    tree.addSplit(event.pos[0],event.pos[1],t)
                    leafs = tree.getLeafs()
                    for leaf in leafs:
                        pygame.draw.line(window, (255, 255, 255), leaf.points['nw'].loc(), leaf.points['ne'].loc())
                        pygame.draw.line(window, (255, 255, 255), leaf.points['ne'].loc(), leaf.points['se'].loc())
                        pygame.draw.line(window, (255, 255, 255), leaf.points['se'].loc(), leaf.points['sw'].loc())
                        pygame.draw.line(window, (255, 255, 255), leaf.points['sw'].loc(), leaf.points['nw'].loc())
                    pygame.display.update()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        if t:
                            tree.cleanMap()
                            t = False
                        else:
                            t = True

if __name__ == "__main__":
    test()
